<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Order\OrderInfoRepository as infoModel;
use App\Repository\Order\OrderLogRepository as logModel;
use App\Repository\Orders\OrdersUserRepository as model;
use App\Repository\Shop\SuppilerRepository as supp;
use App\Repository\TaggingConfig\TaggingConfigRepository;
use Illuminate\Http\Request;

// 分销商
class OrderUserController extends BaseController
{
    protected $repoist = null;
    protected $disModel = null;
    protected $logModel = null;
    protected $infoModel = null;

    public function __construct(model $model, supp $supp, logModel $logModel, infoModel $infoModel)
    {
        $this->repoist = $model;
        $this->disModel = $supp;
        $this->logModel = $logModel;
        $this->infoModel = $infoModel;
    }

    // 根据info 查询订单数据
    public function index(Request $request)
    {
        $where = [];
        $whereWith = [];
        $isTrueAll = true;
        $shop_id = $request->user->shop_id;
        if ($request->has('status') && $request->get('status') != '') {
            // $where['where']['status'] = $request->get('status');
            $whereWith['where']['status'] = $request->get('status');
            // $where['where']['orders_user.status'] = $request->get('status');
        }
        if ($request->has('source') && $request->get('source') != '') {
            $where['where']['source'] = $request->get('source');
        }

        // if (!empty($request->user->role_id) && $request->user->role_id != 1 && empty($request->get('is_dist'))) {
        //   $where['where']['order.shop_id'] = $request->user->shop_id;
        // }
        if ($request->has('three_msg') && $request->get('three_msg') != '') {
            // $where['like']['orders.three_msg'] = $request->get('three_msg');
            $whereWith['like']['three_msg'] = $request->get('three_msg');
        }
        if ($request->has('sell_name') && $request->get('sell_name') != '') {
            // $where['where']['orders_user.sell_name'] = $request->get('sell_name');
            $where['where']['sell_name'] = $request->get('sell_name');
        }
        if (!empty($request->get('is_dist'))) {
            $where['like']['shop_str'] = '.' . $request->user->shop_id . '.';
        }
        if (($request->has('shop_id')) && $request->get('shop_id') == 0) {
            // $where['where']['order.shop_id'] = $request->user->shop_id;
            $isTrueAll = true;
        }
        if (!empty($request->get('shop_id'))) {
            $isTrueAll = false;
            $where['like']['shop_str'] = '.' . $request->user->shop_id . '.' . $request->get('shop_id') . '.';
        }
        // var_dump($where, 111111);
        if (!empty($request->get('orderno'))) {
            $whereWith['where']['orders.ordersno'] = trim($request->get('orderno'), ' ');
        }
        if (!empty($request->get('pay_identity'))) {
            // $where['where']['orders_user.pay_identity'] = trim($request->get('pay_identity'), ' ');
            $where['where']['orders_user.pay_identity'] = trim($request->get('pay_identity'), ' ');
        }
        if (!empty($request->get('sup_orderno'))) {
            $whereWith['where']['orders.sup_orderno'] = $request->get('sup_orderno');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('dist_orderno'))) {
            $where['where']['orders_user.dist_orderno'] = $request->get('dist_orderno');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('iccid'))) {
            $whereWith['where']['orders.iccid'] = $request->get('iccid');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('express_number'))) {
            $whereWith['where']['orders.express_number'] = $request->get('express_number');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('contact_phone'))) {
            $where['where']['contact_phone'] = $request->get('contact_phone');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('contact_name'))) {
            $where['where']['contact_name'] = $request->get('contact_name');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        // if (!empty($request->get('goods_id')) && empty($request->get('is_dist'))) {
        //   $where['where']['order.goods_id'] = $request->get('goods_id');
        //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        // }
        // var_dump($request->get('goods_id'), 1111111111);
        // var_dump($request->get('shop_id'), 2222222222);
        if (!empty($request->get('goods_id')) && empty($request->get('shop_id'))) {
            // $where['where']['order.goods_id'] = $request['goods_id'];
            $whereWith['whereors'][] = [
                'where' => ['goods_id' => $request->get('goods_id')],
                'whereor' => [['sup_goods_id_str', 'like', '%.' . $request->get('goods_id') . '.%']]
            ];
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        // var_dump($whereWith, 44444444);
        if (!empty($request->get('goods_id')) && !empty($request->get('shop_id'))) {
            $whereWith['like']['sup_goods_id_str'] = '%.' . $request->get('goods_id') . '.%';
        }
        // if (!empty($request->get('goods_id')) && !empty($request->get('is_dist'))) {
        //   $where['where']['order.sup_goods_id'] = $request->get('goods_id');
        //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        // }
        if (!empty($request->get('product_id'))) {
            $whereWith['where']['product_id'] = $request->get('product_id');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('channel_id'))) {
            $whereWith['where']['sup_id'] = $request->get('channel_id');
        }
        if (!empty($request->get('author_id'))) {
            $where['where']['author_id'] = $request->get('author_id');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (($request->has('is_trun')) && ($request->get('is_trun') != '')) {
            // $where['where']['type'] = $request->get('is_trun');
            $whereWith['where']['type'] = $request->get('is_trun');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('number'))) {
            $where['like']['name'] = $request->get('number');
        }
        $timeType = 'created_at';
        if (!empty($request->get('time_type'))) {
            switch ($request->get('time_type')) {
                case 1:
                    $timeType = 'created_at';
                    break;
                case 2:
                    $timeType = 'created_at';
                    break;
                case 3:
                    $timeType = 'deliver_at';
                    break;
                case 4:
                    $timeType = 'activat_at';
                    break;
            }
        }
        //时间验证
        if (!empty($request->get('dateRange')) && $request->get('time_type') == 1) {
            // $time = $request->get('dateRange');
            $where['between'] = [$timeType => $request->get('dateRange')];
        }
        if (!empty($request->get('dateRange')) && $request->get('time_type') != 1) {
            // $time = $request->get('dateRange');
            $whereWith['between'] = [$timeType => $request->get('dateRange')];
        }
        // var_dump($where);
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        // DB::connection()->enableQueryLog();
        // $where['join'][] = [];
        // $where['join'][] = [
        //   'table' => 'orders',
        //   'left'  => 'orders_user_id',
        //   'right' => 'orders_user.id'
        // ];
        if ($isTrueAll) {
            $where['whereors'][] = [
                'where' => ['shop_id' => $shop_id],
                'whereor' => [['shop_str', 'like', '%.' . $shop_id . '.%']]
            ];
        }

        // $where['select'] = ['orders.*', 'orders_user.*'];
        // $data =  $this->repoist->index($where);
        // $where = [
        //   'is_trun' => 0
        // ];
        // $whereWith = [
        //   'status' => 0
        // ];
        // var_dump($whereWith, 11111111111);
        // var_dump($where, 22222222);
        $data = $this->repoist->getManyOrders($where, $whereWith);
        // $log = DB::getQueryLog();
        // var_dump($log);
        if (!empty($data['data'])) {
            foreach ($data['data'] as $key => &$val) {
                $val['source'] = '';
                foreach ($val['hasOrders'] as $keys => &$v) {
                    $resSouc = explode('.', $v['shop_name_str']);
                    $resSouId = explode('.', $v['shop_id_str']);
                    if (empty($resSouId)) {
                        continue;
                    }
                    $flag = false;
                    foreach ($resSouId as $keyss => $vt) {
                        if ($keyss == 0 && $vt != $request->get('user')->shop_id) {
                            // $flag = false;
                            $v['sup_name'] = '';
                            $v['sup_orderno'] = '';
                            $flag = true;
                        }
                        if ($keys == 0 && !empty($vt) && $vt == $request->get('user')->shop_id) {
                            $val['source'] = !empty($resSouc[($keyss + 1)]) ? $resSouc[($keyss + 1)] : $val['shop_name'];
                        }
                        if ($flag && !empty($vt) && $vt == $request->get('user')->shop_id) {
                            $v['sup_name'] = !empty($resSouc[($keyss - 1)]) ? $resSouc[($keyss - 1)] : '';
                        }
                    }
                }

                if (empty(env('IS_ENCRYP')) || env('IS_ENCRYP') != 2) {
                    if (mb_strlen($val['contact_name']) <= 2) {
                        $val['contact_name'] = str_replace(mb_substr($val['contact_name'], -1, mb_strlen($val['contact_name']) - 1), '***', ($val['contact_name']));
                    } else {
                        $val['contact_name'] = str_replace(mb_substr($val['contact_name'], -2, mb_strlen($val['contact_name']) - 1), '***', ($val['contact_name']));
                    }
                    if (mb_strlen($val['pay_name']) <= 2) {
                        $val['pay_name'] = str_replace(mb_substr($val['pay_name'], -1, mb_strlen($val['pay_name']) - 1), '***', ($val['pay_name']));
                    } else {
                        $val['pay_name'] = str_replace(mb_substr($val['pay_name'], -2, mb_strlen($val['pay_name']) - 1), '***', ($val['pay_name']));
                    }

                    $val['pay_identity'] = str_replace(mb_substr($val['pay_identity'], 3, 11), '*******', ($val['pay_identity']));
                    $val['pay_phone'] = str_replace(mb_substr($val['pay_phone'], 3, 4), '***', ($val['pay_phone']));
                    $val['contact_phone'] = str_replace(mb_substr($val['contact_phone'], 3, 4), '***', ($val['contact_phone']));
                    $val['post_address'] = str_replace(mb_substr($val['post_address'], 4, mb_strlen($val['post_address']) - 4), '***', ($val['post_address']));
                }

                // if (!empty($val['hasOrders']) && !empty($val['hasOrders'][0]['shop_name_str'])) {
                //   $resSouc = explode('.', $val['hasOrders'][0]['shop_name_str']);
                //   $resSouId = explode('.', $val['hasOrders'][0]['shop_id_str']);
                //   // var_dump($resSouc);
                //   if (empty($resSouId)) {
                //     continue;
                //   }
                //   // if (!empty($resSouId[0]) && !empty($resSouId[0]) != $request->get('user')->shop_id) {
                //   //   $val['source'] = !empty($resSouc[($keys + 1)]) ? $resSouc[($keys + 1)] : $val['shop_name'];
                //   // }
                //   $flag = false;
                //   foreach ($resSouId as $keys => $v) {
                //     if ($keys == 0 && $v != $request->get('user')->shop_id) {
                //       $flag = false;
                //       $val['sup_name'] = '';
                //       $val['sup_orderno'] = '';
                //     }
                //     if (!empty($v) && $v == $request->get('user')->shop_id) {
                //       $val['source'] = !empty($resSouc[($keys + 1)]) ? $resSouc[($keys + 1)] : $val['shop_name'];
                //     }
                //   }
                // }
            }
        }
        return $data;
    }

    // 最初版本 对应的order表的查询
    public function index_back(Request $request)
    {
        $where = [];
        $isTrueAll = false;
        $shop_id = $request->user->shop_id;
        if ($request->has('status') && $request->get('status') != '') {
            $where['where']['orders_user.status'] = $request->get('status');
        }
        if ($request->has('source') && $request->get('source') != '') {
            $where['where']['orders_user.source'] = $request->get('source');
        }

        // if (!empty($request->user->role_id) && $request->user->role_id != 1 && empty($request->get('is_dist'))) {
        //   $where['where']['order.shop_id'] = $request->user->shop_id;
        // }
        if ($request->has('three_msg') && $request->get('three_msg') != '') {
            $where['like']['orders.three_msg'] = $request->get('three_msg');
        }
        if ($request->has('sell_name') && $request->get('sell_name') != '') {
            $where['where']['orders_user.sell_name'] = $request->get('sell_name');
        }
        if (!empty($request->get('is_dist'))) {
            $where['like']['orders.shop_str'] = '.' . $request->user->shop_id . '.';
        }
        if (($request->has('shop_id')) && $request->get('shop_id') == 0) {
            // $where['where']['order.shop_id'] = $request->user->shop_id;
            $isTrueAll = true;
        }
        if (!empty($request->get('shop_id'))) {
            $where['like']['orders.shop_id_str'] = '.' . $request->user->shop_id . '.' . $request->get('shop_id') . '.';
        }
        // var_dump($where, 111111);
        if (!empty($request->get('orderno'))) {
            $where['where']['order.orderno'] = trim($request->get('orderno'), ' ');
        }
        if (!empty($request->get('pay_identity'))) {
            $where['where']['orders_user.pay_identity'] = trim($request->get('pay_identity'), ' ');
        }
        if (!empty($request->get('sup_orderno'))) {
            $where['where']['orders_user.sup_orderno'] = $request->get('sup_orderno');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('dist_orderno'))) {
            $where['where']['orders_user.dist_orderno'] = $request->get('dist_orderno');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('iccid'))) {
            $where['where']['orders_user.iccid'] = $request->get('iccid');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('express_number'))) {
            $where['where']['orders_user.express_number'] = $request->get('express_number');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('contact_phone'))) {
            $where['where']['orders_user.contact_phone'] = $request->get('contact_phone');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('contact_name'))) {
            $where['where']['orders_user.contact_name'] = $request->get('contact_name');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        // if (!empty($request->get('goods_id')) && empty($request->get('is_dist'))) {
        //   $where['where']['order.goods_id'] = $request->get('goods_id');
        //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        // }
        if (!empty($request->get('goods_id')) && empty($request->get('shop_id'))) {
            // $where['where']['order.goods_id'] = $request['goods_id'];
            $where['whereors'] = [
                'where' => ['orders.goods_id' => $request->get('goods_id')],
                'whereor' => [['orders.sup_goods_id_str', 'like', '%.' . $request->get('goods_id') . '.%']]
            ];
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('goods_id')) && !empty($request->get('shop_id'))) {
            $where['like']['orders.sup_goods_id_str'] = '%.' . $request->get('goods_id') . '.%';
        }
        // if (!empty($request->get('goods_id')) && !empty($request->get('is_dist'))) {
        //   $where['where']['order.sup_goods_id'] = $request->get('goods_id');
        //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        // }
        if (!empty($request->get('product_id'))) {
            $where['where']['orders.product_id'] = $request->get('product_id');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('channel_id'))) {
            $where['where']['orders.sup_id'] = $request->get('channel_id');
        }
        if (!empty($request->get('author_id'))) {
            $where['where']['orders_user.author_id'] = $request->get('author_id');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (($request->has('is_trun')) && ($request->get('is_trun') != '')) {
            $where['where']['orders.type'] = $request->get('is_trun');
            // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
        }
        if (!empty($request->get('number'))) {
            $where['like']['orders_user.name'] = $request->get('number');
        }
        $timeType = 'created_at';
        if (!empty($request->get('time_type'))) {
            switch ($request->get('time_type')) {
                case 1:
                    $timeType = 'orders_user.created_at';
                    break;
                case 2:
                    $timeType = 'orders.three_time_at';
                    break;
                case 3:
                    $timeType = 'orders.deliver_at';
                    break;
                case 4:
                    $timeType = 'orders.activat_at';
                    break;
            }
        }
        //时间验证
        if (!empty($request->get('dateRange'))) {
            // $time = $request->get('dateRange');
            $where['between'] = [$timeType => $request->get('dateRange')];
        }
        // var_dump($where);
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        // DB::connection()->enableQueryLog();
        // $where['join'][] = [];
        $where['join'][] = [
            'table' => 'orders',
            'left' => 'orders_user_id',
            'right' => 'orders_user.id'
        ];
        if ($isTrueAll) {
            $where['whereor'] = [
                'where' => ['orders.shop_id' => $shop_id],
                'whereor' => [['orders.shop_str', 'like', '%.' . $shop_id . '.%']]
            ];
        }

        $where['select'] = ['orders.*', 'orders_user.*'];
        $data = $this->repoist->index($where);
        $where = [
            'is_trun' => 0
        ];
        $whereWith = [
            'status' => 0
        ];
        $data = $this->repoist->getManyOrders($where, $whereWith);
        // $log = DB::getQueryLog();
        // var_dump($log);
        return $data;
    }

    // 根据info 查询订单数据
    public function indexDist(Request $request)
    {
        $request->offsetSet('is_dist', 1);
        $res = $this->index($request);
        // $data['']
        if (!empty($res['data'])) {
            foreach ($res['data'] as $key => &$v) {
                $resSouc = explode('.', $v['shop_name_str']);
                $resSouId = explode('.', $v['shop_id_str']);
                $v['source'] = '';
                if (empty($resSouId)) {
                    continue;
                }
                foreach ($resSouId as $keys => $val) {
                    if (!empty($val) && $val == $request->get('user')->shop_id) {
                        $v['source'] = !empty($resSouc[($keys + 1)]) ? $resSouc[($keys + 1)] : $v['shop_name'];
                    }
                }
                if ($resSouId[1] != $request->get('user')->shop_id) {
                    $v['channel_error_josn'] = '[]';
                }
            }
        }
        return $res;
    }

    public function getTagging()
    {
        $repoist = new TaggingConfigRepository();
        return $repoist->getTagging();
    }
}
